﻿using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Core.Scene
{
    public class LoadMetaSceneNode : AbstractSceneNode
    {
        private AsyncOperation asyncOperation;
        private float progress = 0;

        public override void Start()
        {
            if (Scene.CheckMetaSceneIsLoaded())
            {
                IsComplete = true;
            }
            else
            {
                IsComplete = false;
                progress = 0;

                S.mono.StartCoroutine(LoadScene());
            }
        }

        public override void Update()
        {
            if (asyncOperation == null || IsComplete)
            {
                return;
            }

            progress = asyncOperation.progress;

            if (progress >= 0.9f)
            {
                progress = 1.0f;
            }

            if (asyncOperation.isDone && progress == 1)
            {
                IsComplete = true;
            }
        }

        public override void Finish()
        {

        }

        IEnumerator LoadScene()
        {
            asyncOperation = SceneManager.LoadSceneAsync(Scene.MetaScene, LoadSceneMode.Single);
            yield return asyncOperation;
        }

    }
}
